import scrapy


class lunar_spider(scrapy.Spider):
    name = "lunar"
    start_urls = ["https://www.hko.gov.hk/gts/time/calendar/text/T1901c.txt"]

    def parse(self, response):
        page = response.url.split("/")[-1]
        filename = "{}".format(page)
        with open(filename, "wb") as f:
            f.write(response.body)
        self.log('Saved file {}'.format(filename))

        urls = [
            "https://www.hko.gov.hk/gts/time/calendar/text/T{}c.txt".format(k) for k in range(1902, 2101)
        ]
        for url in urls:
            request = scrapy.Request(url=url, callback=self.parse)
            yield request
